<?php
/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/12/17
 * Time: 22:21
 */

namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use Cache;

class MonthFortune extends Base
{
    protected $table = 'month_fortune';
   

    public function getMonthFortune($map = [])
    {
        if (empty($map) || !isset($map['rid']) || !isset($map['month']))
        {
            return [];
        }
        $data = false;

        if(strlen($map['month']) === 6)
        {
            $map['month'] = str_replace('-','-0',$map['month']);
        }

        if (intval($this->cache_on) >= 1 && Cache::has('fortune_month_' . $map['rid'] . '_' . $map['month']))
        {
            $data = Cache::get('fortune_month_' . $map['rid'] . '_' . $map['month']);
        }
        if ($data === false)
        {
            try
            {
                $data = $this->where(['rid' => $map['rid'], 'month' => $map['month']])->firstOrFail();

                // redis 字符串直接存入redis
                $data = $data['fortune'];

                Cache::set('fortune_month_' . $map['rid'] . '_' . $map['month'], $data, 86400 * 7);
            }
            catch (\Exception $e)
            {
                $data = '';
            }
        }
        return $data;
    }

    public function addFortune($insertData = [])
    {
        $status = true;
        if (empty($insertData))
        {
            return false;
        }
        try
        { 
            // 先删除，后添加
            $this->where(['rid' => $insertData['rid'], 'month' => $insertData['month']])->delete();
            $this->updateOrInsert($insertData);
        }
        catch (\Exception $e)
        {
            $status = false;
            \Log::info('月运势写入数据库失败：' . $e->getMessage());
            \Log::info('数据：' . var_export($insertData,true));

        }
        return $status;
    }

    public function addFortuneForYear($insertData = [],$map = [])
    {   
        
        $status = true;
        if (empty($insertData))
        {
            return false;
        }
        try
        {
            // 先删除，后添加 rid=>$rid,month in ('2018-01',2,3,4,5,6,7,8,9,10,11,12)
          //  \Log::info('数据xxxx：' . var_export($map['month'],true));
            //$this->where(['rid'=>$map['rid']])->whereIn('month',$map['month'])->delete();
            //$this->raw($map)->delete();
            // 年运入库的时候，批量入库
              // 删除数据库中已存在的数据
        //  $query = $this->where('rid', $map['rid']);
        // foreach ($map['month'] as $month) {
        //     $query->orWhere('month', $month);
        // }
        // $query->delete();
       //  $this->where($map)->delete();

        // 插入或更新新数据
        // foreach ($insertData as $data) {
       // $this->insert($insertData);
        // }
            //$this->updateOrInsert($insertData);
            foreach ($map as $record) {
                 $existingRecord =  $this->where('rid', $record['rid'])
                                       ->whereIn('month', $record['month'])
                                       ->first();

    if ($existingRecord) {
        // 更新现有记录
        $existingRecord->update(['fortune' => $insertData['fortune']]);
    } else {
        // 插入新记录
        $this->insert($insertData);
    }
}

        }
        catch (\Exception $e)
        {
            $status = false;
            print('年运-月运势写入数据库失败：' . $e->getMessage() . PHP_EOL);
           \Log::info('年运-月运势写入数据库失败：' .$e->getMessage());
           \Log::info('数据：' . var_export($insertData,true));


        }
        return $status;
    }
}
